const formidable = require('formidable');
const fs = require('fs');//?文件夹操作

let server = function (app, db) {

    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from admin_login where AdminCookie=\'' + request.cookies.DOM_Cookie_admin + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //?用户查询
    app.post('/admin_select_user', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('select * from user', function (err, data1) {
                    response.send({ "code": 0, "result": data1 })
                })
            } else {
                response.send({ "code": -1, "result": 'error' })
            }
        })
    })

    //?用户删除
    app.post('/admin/userdelete', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                if (request.body.UserID) {
                    db.query('delete from user where UserID=' + request.body.UserID, function (err, data) {
                        if (err) response.send({ "code": -1, "result": 'error' })
                        else response.send({ "code": 0, "result": 'success' })
                    })
                } else {
                    response.send({ "code": -1, "result": 'error' })
                }
            } else {
                response.send({ "code": -1, "result": 'error' })
            }


        })
    })

    //?新加用户
    app.post('/insert_user', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                var res = /^[0-9]+$/g.test(request.body.UserID)
                if(res){
                    console.log(request.body);
                db.query('insert user(UserID,UserName,UserPassword) values(?,?,?)',
                    [
                        request.body.UserID,
                        request.body.UserName,
                        request.body.UserPassword
                    ], function (err, data) {
                        if (err) response.send({ "code": -1, "result": '账号已存在' })
                        else response.send({ "code": 0, "UserID": request.body.UserID })
                    })
                }else response.send({ "code": -1, "result": '账号只能为纯数字' })
                
            } else {
                response.send({ "code": -1, "result": '请先登录' })
            }
        })
    })

    //?以用户头像接收
    app.post('/admin_userimg_add', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                // ?创建formidable表单解析对象
                const form = new formidable.IncomingForm();
                //?更改文件地址
                form.uploadDir = __dirname + '\\..\\picture'

                //?解析用户传递过来的Formdata对象
                form.parse(request, (err, fields, files) => {
                    // ?修改文件名
                    fs.rename(files.file.filepath, __dirname + '\\..\\picture/' + fields.UserID + '.' + files.file.mimetype.split('/')[1], function () {
                        db.query("update user  set  Portrait='..\\picture/" + fields.UserID + '.' + files.file.mimetype.split('/')[1] + '\'  where UserID=\'' + fields.UserID + '\'', function (err, data) {
                            if (err) console.error(err);
                            response.send({ "code": 0, "result": 'succese' })
                        })
                    })
                });
            } else response.send({ "code": -1, "result": 'error' })
        })
    })
}
module.exports = server
